package cac.fenjian.resource.mspartbasicbao.service;

import cac.common.DateUtil;
import cac.fenjian.resource.MsPart.dto.MsPartBasicDTO;
import cac.fenjian.resource.mspartbasicbao.dao.MsPartBasicDao;
import com.cac.dme.adapter.rest.msg.QueryReqBean;
import com.cac.dme.adapter.rest.msg.QueryRespBean;
import com.cac.dme.adapter.utils.ComUtil;
import com.cac.dme.adapter.utils.PageHelper;
import com.cac.dme.adapter.utils.PojoUtil;
import com.cac.dme.common.exception.service.DaoException;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MsPartService {

    @Autowired
    private MsPartBasicDao msPartBasicDao;


    Date date = new Date();
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String nowTime = sf.format(date);
    Date time = sf.parse(nowTime);

    public MsPartService() throws ParseException {
    }

    /*
    条件查询
    */
    @Transactional(readOnly = true)
    public MsPartBasicDTO searchByProductCode(String productCode) {
        try {
            MsPartBasicDTO msPartBasicDTO = msPartBasicDao.searchByMsPartId(productCode);
            if (msPartBasicDTO != null) {
            }
            return msPartBasicDTO;

        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }

    //分页查询
    @Transactional(readOnly = true)
    public QueryRespBean<MsPartBasicDTO> searchMsPartBasicByPage(QueryReqBean<MsPartBasicDTO> queryReqBean, String productCode, String orderBy) {
        QueryRespBean<MsPartBasicDTO> queryRespBean = new QueryRespBean<>();
        try {
            PageHelper.startPage(queryReqBean.getPageParameter());
            MsPartBasicDTO searchParams = queryReqBean.getSearchParams();
            Page<MsPartBasicDTO> dataList = msPartBasicDao.searchMsPartBasicByPage(searchParams, productCode, orderBy, queryReqBean.getKeyWord());
            queryRespBean.setResult(dataList);
            return queryRespBean;
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }


    //单条插入
    @Transactional
    public String insertMsPartBasic(MsPartBasicDTO msPartBasicDTO) {
        try {
            msPartBasicDTO.setMsPartId(ComUtil.getId());
            msPartBasicDTO.setCreationDate(DateUtil.nowTime());
            msPartBasicDao.insertMsPartBasicDTO(msPartBasicDTO);
            return msPartBasicDTO.getMsPartId();
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }

    //批量新增
    @Transactional
    public int insertMsPartBasic1List(List<MsPartBasicDTO> dtoList) {
        List<MsPartBasicDTO> beanList = new ArrayList<>();
        for (MsPartBasicDTO employeeBasic1DTO : dtoList) {
            employeeBasic1DTO.setMsPartId(ComUtil.getId());
            beanList.add(employeeBasic1DTO);
        }
        try {
            return msPartBasicDao.insertBatchMsPartBasic(beanList);
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }


    //单条更新
    @Transactional
    public int updateMsPartBasic(MsPartBasicDTO msPartBasicDTO) throws ParseException {
        msPartBasicDTO.setLastUpdateDate(DateUtil.nowTime());
        try {
            int count = msPartBasicDao.updateMsPartBasic(getUpdateDto(msPartBasicDTO));
            if (count == 0) {
                throw new DaoException("数据失效，请重新更新");
            }
            return count;
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }

    //批量更新
    @Transactional
    public int updateMsPartBasicList(List<MsPartBasicDTO> dtoList) {
        List<MsPartBasicDTO> beanList = new ArrayList<>();
        for (MsPartBasicDTO msPartBasicDTO : dtoList) {
            MsPartBasicDTO oldDTO = getUpdateDto(msPartBasicDTO);
            beanList.add(oldDTO);
        }
        try {
            return msPartBasicDao.updateMsPartBasicList(beanList);
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }

    //批量更新2
    @Transactional
    public int updateMsPartBasicList2(List<MsPartBasicDTO> dtoList) {
        int count = 0;
        for (MsPartBasicDTO msDTO : dtoList
        ) {
            count = msPartBasicDao.updateMsPartBasic(msDTO);
        }
        return count;
    }


    //自定义方法，获取需要修改的dto对象
    private MsPartBasicDTO getUpdateDto(MsPartBasicDTO msPartBasicDTO) {
        MsPartBasicDTO oldDto = findById(msPartBasicDTO.getMsPartId());
        if (oldDto == null) {
            throw new DaoException("数据不存在");
        }
        PojoUtil.copyProperties(oldDto, msPartBasicDTO, true);
        return oldDto;
    }


    private MsPartBasicDTO findById(String msPartId) {
        return msPartBasicDao.searchByMsPartId(msPartId);
    }


    //删除
    @Transactional
    public int deleteMsPartBasic(String msPartId) {
        if (StringUtils.isEmpty(msPartId)) {
            throw new DaoException("删除失败！传入的参数主键为null");
        }
        try {
            //记录日志
            MsPartBasicDTO msPartBasicDTO = findById(msPartId);
            if (msPartBasicDTO == null) {
                throw new DaoException("删除失败！对象不存在");
            }
            //删除业务数据
            int count = msPartBasicDao.deleteMsPartBasic(msPartId);
            return count;
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }

    //批量删除
    @Transactional
    public int deleteMsPartBasicByIds(String[] ids) {
        int result = 0;
        for (String id : ids) {
            deleteMsPartBasic(id);
            result++;
        }
        return result;
    }

}
